﻿using System;
using System.Collections.Generic;
using Microsoft.SharePoint;

namespace FTC.SharePoint.Evolution.Core
{
    public static class SPProviderConstants
    {
        public const string PROPERTY_NAME = "spevolutions";
    }

    /// <summary>
    /// not abstract, just for SP2010
    /// </summary>
    public class SPProvider
    {
        
        private SPWeb web;

        public SPWeb Web { get { return web; } }

        public SPProvider(SPWeb web)
        {
            this.web = web;
        }

        public List<long> GetAppliedEvolutions()
        {
            return GetAppliedEvolutions(String.Empty);
        }
        public List<long> GetAppliedEvolutions(string key)
        {            
            var applied = new List<long>();
            if (web.AllProperties[SPProviderConstants.PROPERTY_NAME] != null) 
            {
                foreach (string a in (web.AllProperties[SPProviderConstants.PROPERTY_NAME] as string).Split(';'))
                {
                    applied.Add(Convert.ToInt64(a));
                }
            }           
            return applied;
        }

        public void EvolutionsApplied(long version, string key)
        {       
            List<long> appliedToWeb;
            if (web.AllProperties[SPProviderConstants.PROPERTY_NAME] != null)
                appliedToWeb = DeserializeStringToLongList(web.AllProperties[SPProviderConstants.PROPERTY_NAME].ToString());
            else
                appliedToWeb = new List<long> ();
            appliedToWeb.Add(version);
            web.AllProperties[SPProviderConstants.PROPERTY_NAME] = SerializeLongListToString(appliedToWeb);
            web.Update();
        }       
      
        public void EvolutionsUnApplied(long version, string key)
        {
            if (web.AllProperties[SPProviderConstants.PROPERTY_NAME] != null)
            {
                List<long> appliedToWeb = DeserializeStringToLongList(web.AllProperties[SPProviderConstants.PROPERTY_NAME].ToString());
                if (appliedToWeb.Contains(version))
                {
                    appliedToWeb.Remove(version);
                    if (appliedToWeb.Count == 0)
                        web.AllProperties[SPProviderConstants.PROPERTY_NAME] = null;
                    else
                        web.AllProperties[SPProviderConstants.PROPERTY_NAME] = SerializeLongListToString(appliedToWeb);
                    web.Update();
                }
            }         
        }

        #region helpermethods
        public SPList AddGenericList(string name, string description)
        {
            if (this.Web.Lists.TryGetList(name) == null)
                this.Web.Lists.Add(name, description, SPListTemplateType.GenericList);
            return this.Web.Lists.TryGetList(name);
        }

        public void DeleteList(string name)
        {
            SPList list = this.Web.Lists.TryGetList(name);
            if (list != null)
            {
                this.Web.Lists.Delete(list.ID);
            }
           
        }
        #endregion

        public static string SerializeLongListToString(List<long> list)
        {
            return String.Join(";", list.ConvertAll(a => a.ToString()).ToArray());
        }

        public static List<long> DeserializeStringToLongList(string str)
        {
            if (String.IsNullOrEmpty(str))
                return null;
            if (str.Contains(";"))
                return new List<string>(str.Split(';')).ConvertAll(a => Convert.ToInt64(a));
            else  {
                long res = 0;
                Int64.TryParse(str, out res);
                return new List<long>() { res  };
            }
        }
    }
}
